package coop.bancocredicoop.proyectos.gd.alfresco



import spock.lang.*

/**
 *
 */
class AlfrescoHttpClientTest extends GroovyTestCase {
    
    def grailsApplication    

    def setup() {
    }

    def cleanup() {
    }

    def getAlfrescoConfig() {    
        grailsApplication.config.alfresco
        /*
        return [baseURL: "http://10.0.3.42:8080/alfresco",
         rootPath: "/Company Home/Expedientes",
         serviceURL: "/wcservice",
         authentication: [type: "explorer"],
         credentials: [user: "admin",
                       password: "admin"]
        ]
        */
    }

    def getAlfrescoClient() {
        def alfConfig = getAlfrescoConfig() 
        def alfConnection = new AlfrescoServiceConnection(alfConfig)
        alfConnection.getClient(loggedInUser: 'admin')
    }

    void testAddingAValidUserReturnsSuccess() {
        def alfClient = getAlfrescoClient()        
        def result = alfClient.addUserToGroup("testUser1", "GROUP_GD_CARGA")
        
        assertTrue result.success
    }

    void testAddingAValidUserTwiceReturnsSuccess() {
        def alfClient = getAlfrescoClient()        
        
        def result1 = alfClient.addUserToGroup("testUser1", "GROUP_GD_CARGA")
        assertTrue result1.success
        def result2 = alfClient.addUserToGroup("testUser1", "GROUP_GD_CARGA")
        assertTrue result2.success

    }

    void testRemovingAAddedUserReturnsSuccess() {
        def alfClient = getAlfrescoClient()        
        def result1 = alfClient.addUserToGroup("testUser1", "GROUP_GD_CARGA")
        assertTrue result1.success

        def result2 = alfClient.removeUserFromGroup("testUser1", "GROUP_GD_CARGA")
        assertTrue result2.success
       
    }

    void testRemovingAValidUserTwiceReturnsSuccess() {

        def alfClient = getAlfrescoClient()        
        def result1 = alfClient.addUserToGroup("testUser1", "GROUP_GD_CARGA")
        assertTrue result1.success
        def result2 = alfClient.removeUserFromGroup("testUser1", "GROUP_GD_CARGA")
        assertTrue result2.success

        def result3 = alfClient.removeUserFromGroup("testUser1", "GROUP_GD_CARGA")
        assertTrue result3.success
    }

    void testRemovingAnInvalidUserReturnsError() {
        def alfClient = getAlfrescoClient()        
        def result1 = alfClient.removeUserFromGroup("_testUser1", "GROUP_GD_CARGA")
        assertFalse result1.success
    }

    void testRemovingAValidUserFromAnInvalidGroupReturnsError() {
        def alfClient = getAlfrescoClient()        
        def result1 = alfClient.removeUserFromGroup("testUser1", "_GROUP_GD_CARGA")
        assertFalse result1.success
    }

}
